package com.dianxin.smartpark.service.attendance.impl;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkVacationType;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkVacationTypeExample;
import com.dianxin.smartpark.dao.mapper.attendance.SmartParkVacationTypeMapper;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkVacationTypeQO;
import com.dianxin.smartpark.service.attendance.SmartParkVacationTypeService;
import com.dianxin.smartpark.service.vo.attendance.SmartParkVacationTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: smart-park
 * @description:
 * @author: chenyong
 * @create: 2021-07-23 09:47
 */
@Service
public class SmartParkVacationTypeServiceImpl implements SmartParkVacationTypeService {
    private final static Logger logger = LoggerFactory.getLogger(SmartParkVacationTypeServiceImpl.class);

    @Resource
    private SmartParkVacationTypeMapper smartParkVacationTypeMapper;
    /**
     * @Description:  分页查询
     * @Author: ChenYong
     * @Date: 2021/7/23 0023
     * @param smartParkVacationTypeQO:
     * @Return: CommonResponseBody<List<SmartParkVacationTypeVO>>
     */
    @Override
    public CommonResponseBody<List<SmartParkVacationTypeVO>> getVacationTypes(SmartParkVacationTypeQO smartParkVacationTypeQO) {
        SmartParkVacationTypeExample recordExample = new SmartParkVacationTypeExample();
        SmartParkVacationTypeExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIsDeletedEqualTo(0);
        if(StringUtils.isBlank(smartParkVacationTypeQO.getTenantId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.EMPTY_PARAME.getErrorMessage(), ErrMsgEnum.EMPTY_PARAME.getErrorCode());
        }
        if (StringUtils.isNotBlank(smartParkVacationTypeQO.getTenantId())) {
            criteria.andTenantIdEqualTo(smartParkVacationTypeQO.getTenantId());
        }
        if (StringUtils.isNotBlank(smartParkVacationTypeQO.getVacationName())) {
            criteria.andVacationNameLike(smartParkVacationTypeQO.getVacationName());
        }
        if (smartParkVacationTypeQO.getEnableStatus()!=null) {
            criteria.andEnableStatusEqualTo(smartParkVacationTypeQO.getEnableStatus());
        }
        if (smartParkVacationTypeQO.getNeedPage()) {
            recordExample.setOrderByClause("sort desc limit " + (smartParkVacationTypeQO.getPageIndex() - 1) * smartParkVacationTypeQO.getPageSize() + "," + smartParkVacationTypeQO.getPageSize());
        } else {
            recordExample.setOrderByClause("sort desc");
        }

        List<SmartParkVacationType> passRecords = smartParkVacationTypeMapper.selectByExample(recordExample);
        long count = smartParkVacationTypeMapper.countByExample(recordExample);

        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, smartParkVacationTypeQO, SmartParkVacationTypeVO.class);
    }

    /**
     * @Description:  添加
     * @Author: ChenYong
     * @Date: 2021/7/23 0023
     * @param smartParkVacationTypeQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody saveVacationType(SmartParkVacationTypeQO smartParkVacationTypeQO) {
        SmartParkVacationType smartParkVacationType = new SmartParkVacationType();
        try {
            if(StringUtils.isBlank(smartParkVacationTypeQO.getTenantId())){
                return ResponseBodyWrapperUtil.fail(ErrMsgEnum.EMPTY_PARAME.getErrorMessage(), ErrMsgEnum.EMPTY_PARAME.getErrorCode());
            }
            BeanUtil.conversion(smartParkVacationTypeQO,smartParkVacationType);
            smartParkVacationType.beforeInsert();
            return ResponseBodyWrapperUtil.toResData(smartParkVacationTypeMapper.insert(smartParkVacationType));
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @Description:  修改
     * @Author: ChenYong
     * @Date: 2021/7/23 0023
     * @param smartParkVacationTypeQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody updateVacationType(SmartParkVacationTypeQO smartParkVacationTypeQO) {
        SmartParkVacationType smartParkVacationType = new SmartParkVacationType();
        if(StringUtils.isBlank(smartParkVacationTypeQO.getTenantId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.EMPTY_PARAME.getErrorMessage(), ErrMsgEnum.EMPTY_PARAME.getErrorCode());
        }
        try {
            BeanUtil.conversion(smartParkVacationTypeQO,smartParkVacationType);
            smartParkVacationType.beforeUpdate();
            return ResponseBodyWrapperUtil.toResData(smartParkVacationTypeMapper.updateByPrimaryKey(smartParkVacationType));
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }

    }

    /**
     * @Description:  删除
     * @Author: ChenYong
     * @Date: 2021/7/23 0023
     * @param ids:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody deleteVacationType(Long[] ids) {
        int status = 0;
        for (Long id:ids){
            SmartParkVacationType smartParkVacationType =  smartParkVacationTypeMapper.selectByPrimaryKey(id);
            smartParkVacationType.setIsDeleted(1);
            smartParkVacationType.beforeDelete();
            status = smartParkVacationTypeMapper.updateByPrimaryKey(smartParkVacationType);
        }
        return ResponseBodyWrapperUtil.toResData(status);
    }

    /**
     * @Description:  获取假期类型详情
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param smartParkVacationTypeQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody vacationTypeDetails(SmartParkVacationTypeQO smartParkVacationTypeQO) {
        SmartParkVacationTypeVO smartParkVacationTypeVO = new SmartParkVacationTypeVO();
        try {
            SmartParkVacationType smartParkVacationType = smartParkVacationTypeMapper.selectByPrimaryKey(smartParkVacationTypeQO.getId());
            BeanUtil.conversion(smartParkVacationType,smartParkVacationTypeVO);
            return ResponseBodyWrapperUtil.successWrapper(smartParkVacationTypeVO);
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }
}
